package com.xjc.controller;

import com.xjc.common.utils.PageUtils;
import com.xjc.common.utils.R;
import com.xjc.entity.CategoryEntity;
import com.xjc.service.CategoryService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jndi.JndiCallback;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import sun.awt.image.IntegerComponentRaster;

import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 商品三级分类
 *
 * @author xjc
 * @email 2521220414@qq.com
 * @date 2022-09-14 10:07:59
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/test")
    public R test(@Validated(JndiCallback.class) @RequestBody CategoryEntity category){
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam() Map<String, Object> params){
        List<CategoryEntity> list= categoryService.queryCategoryWithChildren();
        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
		categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
